package com.ljcalc.business.components;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;

import com.ljcalc.business.components.requests.HistoryResultRequest;
import com.ljcalc.business.components.requests.SPTTHistoryResultRequest;

public class SPTTHistoryResultsAnalyzer extends HistoryResultsAnalyzer {

	private static SPTTHistoryResultsAnalyzer instance;
	private int startRound = 2351;
	private int current_round = 2353;
	
	
	
	private SPTTHistoryResultsAnalyzer(){
		MINMAX_DIFF_TOLERANCE = 7;
		TIME_CONSECUTIVE_TOLERANCE = 14;
		CAC_NUMBER = 2;
		SUB_COMB_QUANTITY = 15;
		MAX_NUMBER = 45;
		COMB_LENGTH = 6;
		String tc_tolerance = LJCalcProperties.getInstance().getProperty(
		"TIME_CONSECUTIVE_TOLERANCE");
		if (tc_tolerance != null) {
			TIME_CONSECUTIVE_TOLERANCE = Long.valueOf(tc_tolerance).longValue();
		}
		
		resultMap = new HashMap<String,Combination>();

		
		System.out.println(new Date(System.currentTimeMillis()) + ":"+ "Initializing the History Results");
		initHistoryResults();
		
		System.out.println(new Date(System.currentTimeMillis()) + ":"+ "Sorting the result maps");
		resultMap = sortResultMap(resultMap);
		getHistorySubCombinationStat();
		
		minMaxDiffStatList = getHistoryMaxMinDiffStat();
		System.out.println(minMaxDiffStatList);
		minMaxMidDiffStatList = getHistoryMaxMinMidDiffStat();
		System.out.println(minMaxMidDiffStatList);

	}
	
	public static SPTTHistoryResultsAnalyzer getInstance(){
		if(instance==null){
			instance = new SPTTHistoryResultsAnalyzer();
		}
		return instance;
	}
	

	
	public boolean initHistoryResults() {
		
		//from current date get the result type, lordag or onsdag, week and year
		
		//check the local files about the history result until current result type
		theInstance = SPTTResultHistories.getInstance();
		//get the latest date from the result, the date information contains year and week and result type
		HistoryResultRequest lastResultRequest = theInstance.getLatestResultInRequest();
		
		//if there is no data about the result
		if(lastResultRequest==null){
			lastResultRequest = new SPTTHistoryResultRequest(startRound);
		}
		
		return superInit(theInstance, lastResultRequest);
	}
	
	/**
	 * 
	 * @param theindex should be the position of the number in the combination
	 * 			the first one is 0, the last one is 6
	 * @return
	 */
	public List<Integer> getNumberTimeConsecutiveStat(int theindex){

		
		SPTTHistoryResultRequest hrr = new SPTTHistoryResultRequest(startRound);

		List<Combination> firstResults = getResult(hrr.getType(), hrr.getYear(), hrr.getRound());
		List<Integer> retList = getNumberTimeConsecutiveStatSuper(theindex,hrr, firstResults);

		return retList;
	}
	
	
	/*
	 * from the 
	 */
	public HistoryResultRequest getNextResultRequest(HistoryResultRequest r) {
		return new SPTTHistoryResultRequest(r.getRound()+1);

	}
	
	/*
	 * check the result request is valied
	 */
	public boolean isValidRequest(HistoryResultRequest lastResultRequest) {
		HistoryResultRequest hrr = theInstance.getLatestResultInRequest();
		if(hrr==null){
			return lastResultRequest.getRound()<=current_round;
		}
		return lastResultRequest.getRound()<=hrr.getRound();
	}

}
